/*
 * @Date: 2023-03-15 12:05:11
 * @LastEditTime: 2023-06-23 18:48:18

 * 介绍:
 */
/**格式化失败返回数据 */
import { pow, divide, bignumber, multiply } from "mathjs";
import { normalNumber } from "@@/utils/tools/format";

/**
 * 发起支付金额精度调整
 * @param num 原数据
 * @param decimal 小数点右移位数
 * @param disDecimal 是否丢弃小数点
 * @returns
 */
export function parseAmount(
  num: number | string | void | null,
  decimal: number,
  disDecimal = true
): string {
  if (!decimal) return String(num || "");
  const val = bignumber(num || 0);
  const formatNumber = pow(10, decimal);
  const res = normalNumber(multiply(val, formatNumber).toString());
  if (disDecimal) return discardDecimal(res);
  return res;
}

/**
 * 查询余额金额精度调整
 * @param num 原数据
 * @param decimal 小数点左移位数
 * @param disDecimal 是否丢弃小数点
 * @returns
 */
export function parseBalance(
  num: number | string | void | null,
  decimal: number,
  disDecimal = false
): string {
  if (!decimal) return String(num || "");
  const val = bignumber(num || 0);
  const formatNumber = pow(10, decimal);
  const res = normalNumber(divide(val, formatNumber).toString());
  if (disDecimal) return discardDecimal(res);
  return res;
}
export function discardDecimal(num: string | number | void | null) {
  if (!num) return "";
  return String(num).split(".")[0];
}
export function sdkFail<T>(message: string, info?: T): SdkRes<any, T> {
  console.error("sdk失败:" + message, info);
  return {
    message: message || JSON.stringify(info),
    status: false,
    info,
  };
}
/**格式化成功返回数据 */
export function sdkSucceed<T>(message: string, data?: T): SdkRes<T, any> {
  console.info("sdk成功:", message, data);
  return {
    message,
    status: true,
    data,
  };
}

export interface SdkRes<D = any, I = any> {
  message: string;
  /**获取成功失败状态 */
  status: boolean;
  data?: D;
  info?: I;
}
